<?php

namespace service;

use app\admin\model\Store;
use app\api\model\SeparateSet;
use app\api\model\ShareOrder;
use DateTime;
use wechat\WechatController;
use wechat\WxPayV3Controller;

class MysqlJobService
{

    /**
     * 分账详情写入
     * @param $data
     */
    public function share_accounts($data)
    {
        $val = ShareOrder::get(['order_id' => $data['order_id']]);
        if (empty($val)) {
            foreach ($data['data'] as &$datum) {
                $datum['service_secret'] = $data['service_secret'];
                $datum['order_id'] = $data['order_id'];
                $datum['sub_mch_id'] = $data['sub_mch_id'];
            }
            $share_order = new ShareOrder();
            $share_order->insertAll($data['data']);
        }
        return true;
    }

    public function capture_video($data)
    {
        $video = new Video();
        $res = $video->get($data['gather']);
        $arr = json_decode($res, true);
        if ($arr['code'] == 200) {
            $res = $video->sendQi($arr['data']['url']);
            if ($res['code'] == 1) {
                $model = new \app\admin\model\xuboss\Video();
                $model->where(['id' => $data['id']])->update(['url' => $res['data']]);
            }
        }
        return true;
    }

    /**
     * 上传图片到七牛云
     */
    public function upload_file_unlink($data)
    {
        $video = new Video();
        $data['unlink'] = substr($data['unlink'], 1);
        $res = $video->sendImg($data['url'], $data['unlink']);
        unlink(ROOT_PATH . "public{$res['data']}");
        return true;
    }

    /**
     * 绑定用户
     * @param $data
     * @return true
     */
    public function bind_user($data)
    {
        $wechat = new WechatController($data['sub_mch_id'], (object)$data['secret']);
        $receiver = [
            "type" => "PERSONAL_OPENID",
            "account" => $data['openid'],//PERSONAL_OPENID：个人openid
            "relation_type" => "PARTNER"
        ];
        $wechat->addReceiverOpenid($receiver);
        return true;
    }

    /**
     * 创建商家优惠券
     * @param $data
     * @return void
     */
    public function create_card($data)
    {
        // 获取商户信息
        $store = Store::get(['sub_mch_id' => $data['sub_mch_id']]);
        // 获取服务商配置
        $secret = SeparateSet::get(['secret' => $store->service_secret]);
        $secret->sub_mch_id = $store->sub_mch_id;
        $wx = new WxPayV3Controller($secret);
        //如果小于2 则要创建优惠券了
        $dateTime = new DateTime();
        $dateTime->setTimestamp(time());
        // 格式化为RFC3339日期时间格式
        $start_time = $dateTime->format(DateTime::RFC3339);
        $dateTime->setTimestamp(strtotime("+1 year", time()));
        // 格式化为RFC3339日期时间格式
        $end_time = $dateTime->format(DateTime::RFC3339);
        $data = [
            "stock_name" => "20元代金券",
            "belong_merchant" => $store->sub_mch_id,
            "goods_name" => "游玩游戏",
            "stock_type" => "NORMAL",
            "coupon_use_rule" => [
                'coupon_available_time' => [
                    "available_begin_time" => $start_time,
                    'available_end_time' => $end_time,
                ],
                "fixed_normal_coupon" => [
                    "discount_amount" => 500,
                    "transaction_minimum" => 1000,
                ],
                "use_method" => "OFF_LINE",
            ],
            "stock_send_rule" => [
                "max_coupons" => 1000,
                "max_coupons_per_user" => 50,
            ],
            "out_request_no" => date("YmdHis") . mt_rand(100000, 999999),
            "coupon_code_mode" => 'WECHATPAY_MODE',
        ];
        $res = $wx->createCoupon($data);


    }

    /**
     * 领取优惠券
     * @param $data
     */
    public function coupon_receive($data)
    {
        foreach ($data['data'] as $datum) {
            $coupon = new CouponService();
            $res = $coupon->coupon_receive($datum['coupon_id'], $data['user_id'], $datum['coupon_type']);
            if ($res['code'] == 'SUCCESS')
                return true;
            else
                return false;
        }
    }

    /**
     * 微信转账
     */
    public function transfer_bills()
    {
        echo 123;
    }
}